//
//  ProductDetailInfoView.m
//  Castlery
//
//  Created by xie weizhi on 8/9/17.
//  Copyright © 2017 weizhi. All rights reserved.
//

#import "ProductDetailInfoView.h"
#import "Product.h"
#import <YYKit.h>

@implementation ProductDetailInfoView

- (instancetype)init {
    if (self = [super init]) {
        [self configureViews];
    }
    
    return self;
}

- (void)configureViews {
    _productImageView = [UIImageView new];
    _productImageView.translatesAutoresizingMaskIntoConstraints = NO;
    _productImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:_productImageView];
    
    [NSLayoutConstraint
     activateConstraints:@[[_productImageView.topAnchor constraintEqualToAnchor:self.topAnchor],
                           [_productImageView.leftAnchor constraintEqualToAnchor:self.leftAnchor],
                           [_productImageView.rightAnchor constraintEqualToAnchor:self.rightAnchor],
                           [_productImageView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-60.f],]];
    
    _priceLabel = [UILabel new];
    _priceLabel.font = [UIFont boldSystemFontOfSize:12.f];
    _priceLabel.textColor = [UIColor redColor];
    _priceLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_priceLabel];
    
    [NSLayoutConstraint
     activateConstraints:@[[_priceLabel.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:20.f],
                           [_priceLabel.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-10.f]]];
    
    _nameLabel = [UILabel new];
    _nameLabel.font = [UIFont boldSystemFontOfSize:16.f];
    _nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_nameLabel];
    
    [NSLayoutConstraint
     activateConstraints:@[[_nameLabel.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:20.f],
                           [_nameLabel.bottomAnchor constraintEqualToAnchor:_priceLabel.topAnchor constant:-10.f]]];

    _addDateLabel = [UILabel new];
    _addDateLabel.font = [UIFont boldSystemFontOfSize:12.f];
    _addDateLabel.textColor = [UIColor lightGrayColor];
    _addDateLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_addDateLabel];
    
    [NSLayoutConstraint
     activateConstraints:@[[_addDateLabel.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:-20.f],
                           [_addDateLabel.topAnchor constraintEqualToAnchor:_priceLabel.topAnchor]]];
    
    // separator line
    UIView *line = [UIView new];
    line.translatesAutoresizingMaskIntoConstraints = NO;
    line.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:line];
    
    [NSLayoutConstraint
     activateConstraints:@[[line.leftAnchor constraintEqualToAnchor:self.leftAnchor],
                           [line.rightAnchor constraintEqualToAnchor:self.rightAnchor],
                           [line.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
                           [line.heightAnchor constraintEqualToConstant:0.5f],]];
}

- (void)updateViewsWithProduct:(Product *)product {
    _nameLabel.text = product.title;
    _priceLabel.text = product.price;
    _addDateLabel.text = [NSString stringWithFormat:@"Time of sale: %@", product.availableSince];
    
    [_productImageView setImageWithURL:[product firstImageURL] options:YYWebImageOptionProgressive];
}

- (void)updateImageViewWithURL:(NSURL *)url {
    [_productImageView setImageWithURL:url options:YYWebImageOptionProgressive];
}

@end
